/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.farmcode.web.project;

import com.farmcode.api.plugin.ConfigureException;
import com.farmcode.api.plugin.objectinfo.ObjectInfoPluginManager;
import com.farmcode.api.plugin.objectinfo.PluginNotFoundException;
import com.farmcode.api.runtime.FARMCodeContext;
import com.farmcode.api.runtime.Response;
import com.farmcode.api.runtime.RuntimeServerAPI;
import com.farmcode.api.util.FARMCodeAPI;
import com.farmcode.web.BaseFARMCodeBacking;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.bean.RequestScoped;
import javax.inject.Named;

/**
 *
 * @author oscar
 */
@Named("datasourceBackingBean")
@RequestScoped
public class DatasourceBackingBean extends BaseFARMCodeBacking{
    
    public static final String JDBC_DATABASE_EXPLORER = "com.farmcode.plugin.objectinfo.jdbc.JDBCManagerPlugin";
    public static final String JDBC_DATABASE_EXPLORER_SELECTED = "com.farmcode.plugin.objectinfo.jdbc.JDBCManagerPlugin.selected";
    public static final String PLUGINS = DatasourceBackingBean.class.getName() + ".plugins";    
    
    @EJB
    private RuntimeServerAPI runtimeServerAPI;
    
    private String driver;
    private String host;
    private String user;
    private String password;
    private String schema;
    private String selectedPlugin;
    private boolean localDatasource = false;
    private List<ObjectInfoPluginManager> plugins;
    
    public String testConnection(){
        FARMCodeContext context = new FARMCodeContext();
        
        context.put(FARMCodeContext.HOST, getHost());
        context.put(FARMCodeContext.OBJECT_INFO_PLUGIN_MANAGER, getSelectedPlugin());
        context.put(FARMCodeContext.OBJECT_TYPE, "table");
        context.put(FARMCodeContext.DRIVER, getDriver());
        context.put(FARMCodeContext.USER, getUser());
        context.put(FARMCodeContext.PASSWORD, getPassword());
        context.put(FARMCodeContext.SCHEMA, getSchema());
        Response response = runtimeServerAPI.testConection(context);
        List<String> messages = response.getStackTrace();
        
        for (String message : messages) {
            super.showMessage(message);
        }
        
        return null;
        
    }
    
    public void selectedPluginAction(){
        
        if(getSelectedPlugin().equals(JDBC_DATABASE_EXPLORER)){
            setLocalDatasource(true);
        }else{
            setLocalDatasource(false);
        }
    }
    
    public String clear(){
        setDriver(null);
        setHost(null);
        setPassword(null);
        setUser(null);
        setSchema(null);
        return null;
    }
    

    /**
     * @return the driver
     */
    public String getDriver() {
        if(getSession().getValue(FARMCodeContext.DRIVER) != null){
            driver = getSession().getValue(FARMCodeContext.DRIVER);
        }
        return driver;
    }

    /**
     * @param driver the driver to set
     */
    public void setDriver(String driver) {
        this.driver = driver;
        getSession().setValue(FARMCodeContext.DRIVER, driver);
    }

    /**
     * @return the host
     */
    public String getHost() {
        if(getSession().getValue(FARMCodeContext.HOST) != null){
            host = getSession().getValue(FARMCodeContext.HOST);
        }
        return host;
    }

    /**
     * @param host the host to set
     */
    public void setHost(String host) {
        this.host = host;
        getSession().setValue(FARMCodeContext.HOST, host);
    }

    /**
     * @return the user
     */
    public String getUser() {
        if(getSession().getValue(FARMCodeContext.USER) != null){
            user = getSession().getValue(FARMCodeContext.USER);
        }
        return user;
    }

    /**
     * @param user the user to set
     */
    public void setUser(String user) {
        this.user = user;
        getSession().setValue(FARMCodeContext.USER, user);
    }
    /**
     * @return the schema
     */
    public String getSchema() {
        if(getSession().getValue(FARMCodeContext.SCHEMA) != null){
            schema = getSession().getValue(FARMCodeContext.SCHEMA);
        }
        return schema;
    }

    /**
     * @param schema the schema to set
     */
    public void setSchema(String schema) {
        this.schema = schema;
        getSession().setValue(FARMCodeContext.SCHEMA, schema);
    }
    /**
     * @return the password
     */
    public String getPassword() {
         if(getSession().getValue(FARMCodeContext.PASSWORD) != null){
            password = getSession().getValue(FARMCodeContext.PASSWORD);
        }
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
        getSession().setValue(FARMCodeContext.PASSWORD, password);
    }
    
    /**
     * @return the localDatasource
     */
    public boolean isLocalDatasource() {
        if(getSession().getValue(JDBC_DATABASE_EXPLORER_SELECTED) != null)
            localDatasource = Boolean.parseBoolean( getSession().getValue(JDBC_DATABASE_EXPLORER_SELECTED).toString());
        return localDatasource;
    }

    /**
     * @param localDatasource the localDatasource to set
     */
    public void setLocalDatasource(boolean localDatasource) {
        this.localDatasource = localDatasource;
        getSession().setValue(JDBC_DATABASE_EXPLORER_SELECTED, localDatasource);
    }

    /**
     * @return the plugins
     */
    public List<ObjectInfoPluginManager> getPlugins() {
        try {
            if(getSession().getValue(PLUGINS) != null){
                plugins = getSession().getValue(PLUGINS);
            }else {
                setPlugins(FARMCodeAPI.getObjectInfoPlugins());
            }
        } catch (PluginNotFoundException ex) {
            Logger.getLogger(DatasourceBackingBean.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(DatasourceBackingBean.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(DatasourceBackingBean.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(DatasourceBackingBean.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(DatasourceBackingBean.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ConfigureException ex) {
            Logger.getLogger(DatasourceBackingBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        return plugins;
    }

    /**
     * @param plugins the plugins to set
     */
    public void setPlugins(List<ObjectInfoPluginManager> plugins) {
        this.plugins = plugins;
        getSession().setValue(PLUGINS, plugins);
    }

    /**
     * @return the selectedPlugin
     */
    public String getSelectedPlugin() {
        if(getSession().getValue(FARMCodeContext.OBJECT_INFO_PLUGIN_MANAGER) != null){
            selectedPlugin =  getSession().getValue(FARMCodeContext.OBJECT_INFO_PLUGIN_MANAGER);
        }
        return selectedPlugin;
    }

    /**
     * @param selectedPlugin the selectedPlugin to set
     */
    public void setSelectedPlugin(String selectedPlugin) {
        this.selectedPlugin = selectedPlugin;
        getSession().setValue(FARMCodeContext.OBJECT_INFO_PLUGIN_MANAGER, selectedPlugin);
    }
  
}
